﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TinyERP.Framework;
using TinyERP.Framework.Controls;
using Bamboo.Core.Data;
using Bamboo.Core.Common.Collection;
using Bamboo.Core.Common.Logger;


namespace TinyERP.Framework.Controls
{
    public class BaseListForm : BaseForm
    {
        protected IList<SearchCondition> searchConditions;
        protected SortDirection currentSortDirection;
        protected string currentSortField;
        protected Panel searchPanel;
        protected Panel bottomPanel;
        protected Panel controlPanel;
        protected Panel contentPanel;
        protected Panel navigationPanel;
        protected CustomDataGridView dataGridView;
        protected TinyERP.Framework.Controls.DeleteButton deleteButton;
        protected TinyERP.Framework.Controls.CloseButton closeButton;
        protected TinyERP.Framework.Controls.NewButton newButton;
        protected KLabel lblSearch;
        protected TextBox txtDefaultSearchField;
        protected TinyERP.Framework.Controls.FindButton findButton;
        protected TinyERP.Framework.Controls.Paging paging;
        protected FlowLayoutPanel controlFlowLayout;
        protected FlowLayoutPanel flowLayoutSearchPanel;
        public BaseListForm()
        {
            InitializeComponent();
            InitLocalDataVariables();
            WireEvents();
        }
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            
            this.searchPanel = new System.Windows.Forms.Panel();
            this.flowLayoutSearchPanel = new System.Windows.Forms.FlowLayoutPanel();
            this.lblSearch = new KLabel();
            this.txtDefaultSearchField = new System.Windows.Forms.TextBox();
            this.findButton = new TinyERP.Framework.Controls.FindButton();
            this.bottomPanel = new System.Windows.Forms.Panel();
            this.contentPanel = new System.Windows.Forms.Panel();
            this.dataGridView = new CustomDataGridView();
            this.navigationPanel = new System.Windows.Forms.Panel();
            this.paging = new TinyERP.Framework.Controls.Paging();
            this.controlPanel = new System.Windows.Forms.Panel();
            this.controlFlowLayout = new System.Windows.Forms.FlowLayoutPanel();
            this.closeButton = new TinyERP.Framework.Controls.CloseButton();
            this.deleteButton = new TinyERP.Framework.Controls.DeleteButton();
            this.newButton = new TinyERP.Framework.Controls.NewButton();

            this.searchPanel.SuspendLayout();
            this.flowLayoutSearchPanel.SuspendLayout();
            this.bottomPanel.SuspendLayout();
            this.contentPanel.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit();
            this.navigationPanel.SuspendLayout();
            this.controlPanel.SuspendLayout();
            this.controlFlowLayout.SuspendLayout();
            this.SuspendLayout();
            // 
            // searchPanel
            // 
            this.searchPanel.Controls.Add(this.flowLayoutSearchPanel);
            this.searchPanel.Dock = System.Windows.Forms.DockStyle.Top;
            this.searchPanel.Location = new System.Drawing.Point(0, 0);
            this.searchPanel.Name = "searchPanel";
            this.searchPanel.Size = new System.Drawing.Size(633, 37);
            this.searchPanel.TabIndex = 0;
            // 
            // flowLayoutSearchPanel
            // 
            this.flowLayoutSearchPanel.Controls.Add(this.lblSearch);
            this.flowLayoutSearchPanel.Controls.Add(this.txtDefaultSearchField);
            this.flowLayoutSearchPanel.Controls.Add(this.findButton);
            this.flowLayoutSearchPanel.Dock = System.Windows.Forms.DockStyle.Left;
            this.flowLayoutSearchPanel.Location = new System.Drawing.Point(0, 0);
            this.flowLayoutSearchPanel.Name = "flowLayoutSearchPanel";
            this.flowLayoutSearchPanel.Size = new System.Drawing.Size(615, 37);
            this.flowLayoutSearchPanel.TabIndex = 3;
            // 
            // lblSearch
            // 
            this.lblSearch.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lblSearch.Location = new System.Drawing.Point(3, 0);
            this.lblSearch.Name = "lblSearch";
            this.lblSearch.Size = new System.Drawing.Size(150, 29);
            this.lblSearch.TabIndex = 0;
            this.lblSearch.Text = AppResourceManager.GetInstance().GetString("Search");
            this.lblSearch.ResourceKey = "Search";
            this.lblSearch.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // txtDefaultSearchField
            // 
            this.txtDefaultSearchField.Location = new System.Drawing.Point(159, 3);
            this.txtDefaultSearchField.Name = "txtDefaultSearchField";
            this.txtDefaultSearchField.Size = new System.Drawing.Size(349, 20);
            this.txtDefaultSearchField.TabIndex = 1;
            // 
            // findButton
            // 
            this.findButton.Location = new System.Drawing.Point(514, 3);
            this.findButton.Name = "findButton";
            this.findButton.TabIndex = 2;



            // 
            // bottomPanel
            // 
            this.bottomPanel.Controls.Add(this.contentPanel);
            this.bottomPanel.Controls.Add(this.controlPanel);
            this.bottomPanel.Dock = System.Windows.Forms.DockStyle.Fill;
            this.bottomPanel.Location = new System.Drawing.Point(0, 37);
            this.bottomPanel.Name = "bottomPanel";
            this.bottomPanel.Size = new System.Drawing.Size(633, 299);
            this.bottomPanel.TabIndex = 1;
            // 
            // contentPanel
            // 
            this.contentPanel.Controls.Add(this.dataGridView);
            this.contentPanel.Controls.Add(this.navigationPanel);
            this.contentPanel.Dock = System.Windows.Forms.DockStyle.Fill;
            this.contentPanel.Location = new System.Drawing.Point(0, 0);
            this.contentPanel.Name = "contentPanel";
            this.contentPanel.Size = new System.Drawing.Size(633, 263);
            this.contentPanel.TabIndex = 1;
            // 
            // dataGridView
            // 
            this.dataGridView.AllowUserToAddRows = false;
            this.dataGridView.AllowUserToDeleteRows = false;
            this.dataGridView.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
            this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;



            this.dataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
            this.dataGridView.Location = new System.Drawing.Point(0, 0);
            this.dataGridView.Name = "dataGridView";
            this.dataGridView.AutoGenerateColumns = false;
            this.dataGridView.ReadOnly = true;
            this.dataGridView.Size = new System.Drawing.Size(633, 233);
            this.dataGridView.TabIndex = 1;
            // 
            // navigationPanel
            // 
            this.navigationPanel.Controls.Add(this.paging);
            this.navigationPanel.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.navigationPanel.Location = new System.Drawing.Point(0, 233);
            this.navigationPanel.Name = "navigationPanel";
            this.navigationPanel.Size = new System.Drawing.Size(633, 30);
            this.navigationPanel.TabIndex = 0;
            // 
            // paging
            // 
            this.paging.AutoSize = true;
            this.paging.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.paging.Dock = System.Windows.Forms.DockStyle.Fill;
            this.paging.Location = new System.Drawing.Point(0, 0);
            this.paging.Name = "paging";
            this.paging.Size = new System.Drawing.Size(633, 30);
            this.paging.TabIndex = 0;
            // 
            // controlPanel
            // 
            this.controlPanel.Controls.Add(this.controlFlowLayout);
            this.controlPanel.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.controlPanel.Location = new System.Drawing.Point(0, 263);
            this.controlPanel.Name = "controlPanel";
            this.controlPanel.Size = new System.Drawing.Size(633, 36);
            this.controlPanel.TabIndex = 0;
            // 
            // controlFlowLayout
            // 
            this.controlFlowLayout.Controls.Add(this.closeButton);
            this.controlFlowLayout.Controls.Add(this.deleteButton);
            this.controlFlowLayout.Controls.Add(this.newButton);
            this.controlFlowLayout.Dock = System.Windows.Forms.DockStyle.Fill;
            this.controlFlowLayout.FlowDirection = System.Windows.Forms.FlowDirection.RightToLeft;
            this.controlFlowLayout.Location = new System.Drawing.Point(0, 0);
            this.controlFlowLayout.Name = "controlFlowLayout";
            this.controlFlowLayout.Margin = new System.Windows.Forms.Padding(1);
            this.controlFlowLayout.Padding = new System.Windows.Forms.Padding(1);
            this.controlFlowLayout.Size = new System.Drawing.Size(633, 36);
            this.controlFlowLayout.TabIndex = 2;

            // 
            // newButton
            // 
            this.newButton.Location = new System.Drawing.Point(387, 6);
            this.newButton.Name = "newButton";
            this.newButton.TabIndex = 5;
            // 
            // deleteButton
            // 
            this.deleteButton.Location = new System.Drawing.Point(468, 6);
            this.deleteButton.Name = "deleteButton";
            this.deleteButton.TabIndex = 6;
            // 
            // closeButton
            // 
            this.closeButton.Location = new System.Drawing.Point(549, 6);
            this.closeButton.Name = "closeButton";

            this.closeButton.TabIndex = 7;
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.Controls.Add(this.bottomPanel);
            this.Controls.Add(this.searchPanel);

            this.Size = new System.Drawing.Size(633, 336);
            this.searchPanel.ResumeLayout(false);
            this.flowLayoutSearchPanel.ResumeLayout(false);
            this.flowLayoutSearchPanel.PerformLayout();
            this.bottomPanel.ResumeLayout(false);
            this.contentPanel.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit();
            this.navigationPanel.ResumeLayout(false);
            this.navigationPanel.PerformLayout();
            this.controlPanel.ResumeLayout(false);
            this.controlFlowLayout.ResumeLayout(false);
            this.ResumeLayout(false);
        }

        #endregion

        protected virtual void InitLocalDataVariables()
        {
            searchConditions = new List<SearchCondition>();
        }
        public override void RegisterShortcuts()
        {
            ShortcutHandler.GetInstance().RegisterShortcut(StandardShortcutKey.NEW, this, "OnNew");
            ShortcutHandler.GetInstance().RegisterShortcut(StandardShortcutKey.EDIT, this, "OnEdit");
            ShortcutHandler.GetInstance().RegisterShortcut(StandardShortcutKey.CLOSE, this, "OnClose");
            ShortcutHandler.GetInstance().RegisterShortcut(StandardShortcutKey.DELETE, this, "OnDelete");
            ShortcutHandler.GetInstance().RegisterShortcut(StandardShortcutKey.FIND, this, "OnFind");
        }

        private void WireEvents()
        {
            this.Load += delegate(object sender, EventArgs e)
            {
                OnLoad();
            };
            this.dataGridView.ColumnSortChanged += new CustomDataGridViewSortEventHandler(OnDataGridViewColumnSortChanged);
            this.paging.OnPageChanged += new PageChangedHandler(OnPageChanged);
            this.newButton.Click += delegate(object senderObj, EventArgs ev)
            {
                OnNew();
            };
            this.closeButton.Click += delegate(object senderObj, EventArgs ev)
            {
                OnClose();
            };
            this.deleteButton.Click += delegate(object senderObj, EventArgs ev)
            {
                OnDelete();
            };
            this.findButton.Click += delegate(object senderObj, EventArgs ev)
            {
                OnFind();
            };
            this.dataGridView.DoubleClick += delegate(object senderObj, EventArgs ev)
            {
                OnEdit();
            };
        }

        public void OnLoad()
        {
            LoadGridData();
        }

        public virtual void OnNew()
        {
        }

        public virtual void OnClose()
        {
            InvokeCloseDialog();
        }

        public virtual void OnDelete()
        {
            
        }

        public virtual void OnFind()
        {
            LoadGridData();
        }

        public virtual void OnEdit()
        {

        }

        protected virtual void BuildDefaultSearchConditions()
        {
            searchConditions = new List<SearchCondition>();
            SearchCondition condition = new SearchCondition();
            condition.SearchField = "Name";
            condition.SearchParameterName = "Name";
            condition.SearchOperator = SearchOperator.LIKE;
            string defaultSearchFieldValue = txtDefaultSearchField.Text.Trim();
            condition.SearchParameterValue = "%" + defaultSearchFieldValue + "%";
            searchConditions.Add(condition);
        }

        protected virtual void LoadGridData()
        {
            string defaultSearchFieldValue = txtDefaultSearchField.Text.Trim();

            if (!string.IsNullOrEmpty(defaultSearchFieldValue))
            {
                BuildDefaultSearchConditions();
            }
            if (searchConditions.Count == 0)
            {
                LoadGridViewDefaultData(currentSortDirection, currentSortField);
            }
            else
            {
                LoadGridViewSearchData(searchConditions, currentSortDirection, currentSortField);
            }
        }

        protected virtual void LoadGridViewSearchData(IList<SearchCondition> searchConditions, Bamboo.Core.Data.SortDirection sortDirection, string columnName)
        {
        }

        protected virtual void LoadGridViewDefaultData(Bamboo.Core.Data.SortDirection sortDirection, string columnName)
        {
        }

        protected virtual void OnDataGridViewColumnSortChanged(object sender, SortColumnChangedEvent e)
        {
            currentSortDirection = e.SortDirection;
            currentSortField = e.ColumnName;
            paging.CurrentPage = 0;
            LoadGridData();
        }

        protected virtual void OnPageChanged(object sender, PagingArg arg)
        {
            LoadGridData();
        }
    }
}
